package com.fhj.movie.adapter;

import java.util.List;

import com.fhj.movie.entity.JyjCinemaEntity;
import com.fhj.movie.entity.JyjDistrictEntity;
import com.fhj.movie_fhj_01.R;
import com.fhj.utils.CalculatorDistance;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CinemaListAdapter extends BaseAdapter {

	private List<JyjCinemaEntity> list;
	private Context context;
	private List<JyjDistrictEntity> districts;
	
	private double localLatitude;
	private double localLongitude;

	public void initData(Context context, List<JyjCinemaEntity> list,
			List<JyjDistrictEntity> districts,double localLatitude,double  localLongitude) {
		this.list = list;
		this.context = context;
		this.districts = districts;
		this.localLatitude = localLatitude;
		this.localLongitude =localLongitude;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.movie_listview_item, parent, false);
			holder.filmimax = (ImageView) convertView
					.findViewById(R.id.movie_listview_filmimax);
			holder.grade = (TextView) convertView
					.findViewById(R.id.movie_listview_grade);
			holder.name = (TextView) convertView
					.findViewById(R.id.movie_listview_filmname);
			holder.price = (TextView) convertView
					.findViewById(R.id.movie_listview_price);
			holder.quan = (ImageView) convertView
					.findViewById(R.id.movie_listview_quan);
			holder.screenings = (TextView) convertView
					.findViewById(R.id.movie_listview_screenings);
			holder.seat = (ImageView) convertView
					.findViewById(R.id.movie_listview_seat);
			holder.area = (TextView) convertView
					.findViewById(R.id.movie_listview_area);
			holder.locationInfo = (TextView)convertView
					.findViewById(R.id.movie_listview_locationInfo);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		JyjCinemaEntity cinema = list.get(position);

		if (cinema.isFlags()) {
			holder.area.setVisibility(View.VISIBLE);
			JyjDistrictEntity entity = null;
			for (int i = 0; i < districts.size(); i++) {
				entity = districts.get(i);
				if (entity.getId() == cinema.getDistrictId()) {
					holder.area.setText(entity.getName());
				}
			}
		} else {
			holder.area.setVisibility(View.GONE);
		}

		holder.name.setText(cinema.getName());
		if (cinema.getisImaxSupport() == 1) {
			holder.filmimax.setVisibility(View.VISIBLE);
		}
		holder.grade.setText(cinema.getGrade() + "分");
		holder.screenings.setText(cinema.getScreenings());
		if (cinema.getLowprice() > 0.0) {
			holder.price.setVisibility(View.VISIBLE);
			holder.price.setText("￥" + cinema.getLowprice() + "起");
		} else {
			holder.price.setVisibility(View.GONE);
		}
		if (cinema.getIsSeatSupport() == 1) {
			holder.seat.setVisibility(View.VISIBLE);
		}
		if (cinema.getIsCouponSupport() == 1) {
			holder.quan.setVisibility(View.VISIBLE);
		}
		if (cinema.getCoord()!=null) {
			String s  = cinema.getCoord().trim();
			String [] ss = s.split(",");
			double  latitude= Double.parseDouble(ss[1]);
			double  longitude= Double.parseDouble(ss[0]);
			double distance = CalculatorDistance.calculate(localLatitude, localLongitude, latitude, longitude);
			holder.locationInfo.setText(distance+"km");
		}
		return convertView;
	}

	private class ViewHolder {
		private TextView name;
		private ImageView filmimax;
		private TextView grade;
		private TextView  locationInfo;
		private TextView screenings;
		private TextView price;
		private ImageView quan;
		private ImageView seat;
		private TextView area;
	}
}